import page from '../../databus/page.js'
import usersAddTpl from '../../views/users-add.art'
import { usersAdd as usersAddModel } from '../../models/users-add'

// 添加用户
export const addUser = () => {
  // return (e) => {
  //   console.log(222)
  // }
  
  const html = usersAddTpl({})
  $('#users-list-box').after(html)
    
  const $btnClose = $('#users-close')

  const _save = async () => {
    // 提交表单
    const data = $('#users-form').serialize()
    let res = await usersAddModel(data)
    if(res.ret) {
      alert(res.data.message)
      page.setCurPage(1)
      // _loadData() // 添加数据后渲染
      $('body').trigger('addUser')
      $btnClose.click()
      $('input[name="username"]').val('')
      $('input[name="password"]').val('')
    } else {
      alert(res.data.message)
    }
  }
    
  // 点击保存，提交表单
  $('#users-save').on('click',  _save)
}
